package com.example.smartf.controller;

import com.example.smartf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/*
    用户管理
    关于用户账户的管理操作
     控制层
 */
@Api("用户登录管理")
@CrossOrigin(origins = "*")
@RestController
public class UserController {
    @Autowired
    private UserService userService;






    //发送验证码
    @ApiOperation("用户注册发送验证码")
    @ApiImplicitParam(name = "username", value = "用户名（邮箱号）", dataType = "String",paramType = "query")
    @PostMapping(value = "/user/regsendcode")
    public String sendcode(@RequestParam("username") String username){
        return userService.regsendCode(username);
    }










    //用户注册
    @ApiOperation("用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名（邮箱号）", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/user/reg")
    public String addUser(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam("code") String code){
        return userService.addUser(username,password,code);
    }








    //用户登录
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名（邮箱号）", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password){
        return userService.login(username,password);
    }








    //忘记密码-发送验证码
    @ApiOperation("用户忘记密码发送验证码")
    @ApiImplicitParam(name = "username", value = "用户名（邮箱号）", dataType = "String",paramType = "query")
    @PostMapping(value = "/user/resetpwdsendcode")
    public String resetpwdsendcode(@RequestParam("username") String username){
        return userService.resetpwdsendCode(username);
    }






    //忘记密码 重置密码
    @ApiOperation("用户忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名（邮箱号）", dataType = "String"),
            @ApiImplicitParam(name = "newpassword", value = "用户新密码", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String")
    })
    @PostMapping(value = "/user/resetpwd")
    public String resetpwd(@RequestParam("username") String username,
                           @RequestParam("newpassword") String newpassword,
                           @RequestParam("code") String code){
        return userService.resetpwd(username,newpassword,code);
    }

}
